# -*- coding: UTF-8 -*-
# 生成所有图片相似度矩阵
from MLKNN import *

# 定义相似度矩阵
matchscores = np.zeros((all_set_len, all_set_len))


# 比较两张图片之间相似度的函数
def distance(feat1, feat2):
    l1, d1 = sift.read_features_from_file(feat1)
    l2, d2 = sift.read_features_from_file(feat2)
    matches = sift.match_twosided(d1, d2)
    matches = sum(matches > 0)
    return matches


# 将shot路径转化为sift函数可读取的sift路径
def get_feature(shotname):
    featname = shotname.replace('.jpg', '.sift')
    featname = 'G:/TestResportsGeneration/screenshots/' + featname
    return featname


# 遍历测试集，逐个与所有训练集进行对比，生成相似性矩阵，只计算上三角
count = 0
for i in range(all_set_len):
    for j in range(i, all_set_len):

        featname1 = get_feature(shotslist[i].shotname)
        featname2 = get_feature(shotslist[j].shotname)
        matchscores[i][j] = distance(featname1, featname2)
        print 'Comaring:' + str(count) + ' / ' + str(((all_set_len * all_set_len) + all_set_len) / 2) + ' No:' + str(i) \
              + ' with ' + 'No:' + str(j) + ' Matchscore:' + str(matchscores[i][j])
        count += 1

# 复制上三角的值，生成全部矩阵
for i in range(all_set_len):
    for j in range(i + 1, all_set_len):  # no need to copy diagonal
        matchscores[j, i] = matchscores[i, j]

np.savetxt('matchscores.txt', matchscores)
print 'matchscores.txt saved!!!'

